package com.mall.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.mall.db.api.SmsFlashPromotionProductRelationApi;
import com.mall.db.api.SmsFlashPromotionSessionApi;
import com.mall.exception.ConsumerException;
import com.mall.pojo.SmsFlashPromotionSession;
import com.mall.vo.FlashSessionSelectListVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FlashSessionService {

    @DubboReference
    private SmsFlashPromotionSessionApi sessionApi;

    @DubboReference
    private SmsFlashPromotionProductRelationApi relationApi;

    /**
     * 获取所有秒杀时间段
     *
     * @return
     */
    public List<SmsFlashPromotionSession> list() {
        return sessionApi.list();
    }

    /**
     * 添加秒杀场次
     *
     * @param promotionSession
     * @return
     */
    public Integer create(SmsFlashPromotionSession promotionSession) {
        //秒杀时间严谨性判断
        Date endTime = promotionSession.getEndTime();
        Date startTime = promotionSession.getStartTime();
        if (ObjectUtil.isNotNull(endTime) && ObjectUtil.isNotNull(startTime)) {
            if (endTime.getTime() - startTime.getTime() <= 0) {
                throw new ConsumerException("场次时间设置错误");
            }
        }
        //添加秒杀活动场次
        promotionSession.setCreateTime(new Date(System.currentTimeMillis()));
        Integer count = sessionApi.create(promotionSession);
        return count;
    }

    /**
     * 删除秒杀场次
     *
     * @param id
     * @return
     */
    public Integer delete(Integer id) {
        //根据id查询秒杀场次信息
        SmsFlashPromotionSession session = sessionApi.findById(id);
        //启用的场次不可被删除
        if (session.getStatus() == 1) {
            throw new ConsumerException("启用场次不可被删除");
        }
        Integer count = sessionApi.delete(id);
        return count;
    }

    /**
     * 修改秒杀场次
     *
     * @param id
     * @param promotionSession
     * @return
     */
    public Integer update(Integer id, SmsFlashPromotionSession promotionSession) {
        //秒杀时间严谨性判断
        Date endTime = promotionSession.getEndTime();
        Date startTime = promotionSession.getStartTime();
        if (ObjectUtil.isNotNull(endTime) && ObjectUtil.isNotNull(startTime)) {
            if (endTime.getTime() - startTime.getTime() <= 0) {
                throw new ConsumerException("场次时间设置错误");
            }
        }

        if (ObjectUtil.isNull(promotionSession.getId())) {
            promotionSession.setId(Convert.toLong(id));
        }
        //修改
        Integer count = sessionApi.update(promotionSession);
        return count;
    }

    /**
     * 修改秒杀场次启用状态
     *
     * @param id
     * @param status
     * @return
     */
    public Integer updateStatus(Integer id, Integer status) {
        //根据id查询秒杀场次信息
        SmsFlashPromotionSession session = sessionApi.findById(id);
        //修改启用状态
        session.setStatus(status);
        Integer count = sessionApi.update(session);
        return count;
    }

    /**
     * 获取全部可选场次及其数量,场次处于启用状态
     * @param flashPromotionId 秒杀活动id
     * @return
     */
    public List<FlashSessionSelectListVo> selectList(Integer flashPromotionId) {
        List<FlashSessionSelectListVo> list = new ArrayList<>();
        //获取全部处于启用状态的秒杀场次
        List<SmsFlashPromotionSession> sessionList = sessionApi.findByStatus(1);
        //判断sessionList是否为空
        if (CollUtil.isEmpty(sessionList)){
            return list;
        }
        //根据秒杀活动id和场次id查询商品数量
        list = sessionList.stream().map(session->{
            //获取场次id
            Long sessionId = session.getId();
            Integer mount = relationApi.findMount(sessionId,flashPromotionId);
            //组装数据
            FlashSessionSelectListVo vo = new FlashSessionSelectListVo();
            BeanUtils.copyProperties(session,vo);
            vo.setProductCount(mount);
            return vo;
        }).collect(Collectors.toList());

        return list;
    }
}
